package net.chrisjrock.display
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	public class ScaleNiner extends Sprite
	{
		protected var _xSize:int;
		protected var _ySize:int;
		
		protected var _cornerTopLeft:BitmapData;
		protected var _cornerTopRight:BitmapData;
		protected var _cornerBotLeft:BitmapData;
		protected var _cornerBotRight:BitmapData;
		
		protected var _top:BitmapData;
		protected var _left:BitmapData;
		protected var _right:BitmapData;
		protected var _bot:BitmapData;
		
		protected var _fill:BitmapData;
		
		public function ScaleNiner(cornerTopLeft:BitmapData , cornerTopRight:BitmapData , cornerBotLeft:BitmapData , cornerBotRight:BitmapData , top:BitmapData , left:BitmapData , right:BitmapData , bot:BitmapData , fill:BitmapData = null)
		{
			setBitmapData
			(
				cornerTopLeft ,
				cornerTopRight ,
				cornerBotLeft ,
				cornerBotRight ,
				top ,
				left ,
				right ,
				bot ,
				fill
			);
		}
		
		public function destroy ():void
		{
			_xSize = _ySize = 0;
			_cornerTopLeft = null;
			_cornerTopRight = null;
			_cornerBotLeft = null;
			_cornerBotRight = null;
			_top = null;
			_left = null;
			_right = null;
			_bot = null;
		}
		
		public function disposeAll ():void
		{
			_cornerTopLeft.dispose();
			_cornerTopLeft = null;
			_cornerTopRight.dispose();
			_cornerTopRight = null;
			_cornerBotLeft.dispose();
			_cornerBotLeft = null;
			_cornerBotRight.dispose();
			_cornerBotRight = null;
			_top.dispose();
			_top = null;
			_left.dispose();
			_left = null;
			_right.dispose();
			_right = null;
			_bot.dispose();
			_bot = null;
		}
		
		public function setBitmapData (cornerTopLeft:BitmapData , cornerTopRight:BitmapData , cornerBotLeft:BitmapData , cornerBotRight:BitmapData , top:BitmapData , left:BitmapData , right:BitmapData , bot:BitmapData , fill:BitmapData = null):void
		{
			_cornerTopLeft = cornerTopLeft;
			_cornerTopRight = cornerTopRight;
			_cornerBotLeft = cornerBotLeft;
			_cornerBotRight = cornerBotRight;
			_top = top;
			_left = left;
			_right = right;
			_bot = bot;
			_fill = fill;
			
			setSize(_xSize , _ySize);
		}
		
		public function setSize (xSize:int , ySize:int):void
		{
			if (!canDraw())
			{
				return;
			}
			
			_xSize = xSize;
			_ySize = ySize;
			
			var sizeMin:Point = getSizeMin();
			
			if (_xSize < sizeMin.x)
			{
				_xSize = sizeMin.x;
			}
			
			if (_ySize < sizeMin.y)
			{
				_ySize = sizeMin.y;
			}
			
			graphics.clear();
			graphics.lineStyle(0 , 0 , 0);
			
			if (_fill)
			{
				graphics.beginBitmapFill(_fill , null , true);
				graphics.drawRect(0 , 0 , _xSize , _ySize);
			}
			
			var matrix:Matrix = new Matrix();
			
			graphics.beginBitmapFill(_cornerTopLeft , null , false);
			graphics.drawRect(0 , 0 , _cornerTopLeft.width , _cornerTopLeft.height);
			
			matrix.tx = _xSize - _cornerTopRight.width;
			matrix.ty = 0;
			graphics.beginBitmapFill(_cornerTopRight , matrix , false);
			graphics.drawRect(matrix.tx , matrix.ty , _cornerTopRight.width , _cornerTopRight.height);
			
			matrix.tx = 0;
			matrix.ty = _ySize - _cornerBotLeft.height;
			graphics.beginBitmapFill(_cornerBotLeft , matrix , false);
			graphics.drawRect(matrix.tx , matrix.ty , _cornerBotLeft.width , _cornerBotLeft.height);
			
			matrix.tx = _xSize - _cornerTopRight.width;
			matrix.ty = _ySize - _cornerBotRight.height;
			graphics.beginBitmapFill(_cornerBotRight , matrix , false);
			graphics.drawRect(matrix.tx , matrix.ty , _cornerBotRight.width , _cornerBotRight.height);
			
			matrix.tx = _cornerTopLeft.width;
			matrix.ty = 0;
			graphics.beginBitmapFill(_top , matrix , true);
			graphics.drawRect(matrix.tx , matrix.ty , _xSize - _cornerTopLeft.width - _cornerTopRight.width , _top.height);
			
			matrix.tx = 0;
			matrix.ty = _cornerTopLeft.height;
			graphics.beginBitmapFill(_left , matrix , true);
			graphics.drawRect(matrix.tx , matrix.ty , _left.width , _ySize - _cornerTopLeft.height - _cornerBotLeft.height);
			
			matrix.tx = _xSize - _right.width;
			matrix.ty = _cornerTopRight.height;
			graphics.beginBitmapFill(_right , matrix , true);
			graphics.drawRect(matrix.tx , matrix.ty , _right.width , _ySize - _cornerTopRight.height - _cornerBotRight.height);
			
			matrix.tx = _cornerBotLeft.width;
			matrix.ty = _ySize - _bot.height;
			graphics.beginBitmapFill(_bot , matrix , true);
			graphics.drawRect(matrix.tx , matrix.ty , _xSize - _cornerBotLeft.width - _cornerBotRight.width , _bot.height);
		}
		
		public function canDraw ():Boolean
		{
			return _cornerTopLeft && _cornerTopRight && _cornerBotLeft && _cornerBotRight && _top && _left && _right && _bot;
		}
		
		public function getSizeMin ():Point
		{
			if (!canDraw())
			{
				return null;
			}
			
			var xSizeMin:int = _cornerTopLeft.width + _cornerTopRight.width;
			if (xSizeMin < _cornerBotLeft.width + _cornerBotRight.width)
			{
				xSizeMin = _cornerBotLeft.width + _cornerBotRight.width;
			}
			
			var ySizeMin:int = _cornerTopLeft.height + _cornerBotLeft.height;
			if (ySizeMin < _cornerTopRight.width + _cornerBotRight.width)
			{
				ySizeMin = _cornerTopRight.width + _cornerBotRight.width;
			}
			
			return new Point(xSizeMin , ySizeMin);
		}
		
		public function getFrameThickness (side:int):int
		{
			var thickness:int;
			switch (side)
			{
				case 0: //Right
					if (_cornerTopRight.width >= _cornerBotRight.width)
					{
						thickness = _cornerTopRight.width;
					}
					else
					{
						thickness = _cornerBotRight.width;
					}
					
					if (thickness < _right.width)
					{
						thickness = _right.width;
					}
					return thickness;
					
				case 1: //Bottom
					if (_cornerBotLeft.height >= _cornerBotRight.height)
					{
						thickness = _cornerBotLeft.height;
					}
					else
					{
						thickness = _cornerBotRight.height;
					}
					
					if (thickness < _top.height)
					{
						thickness = _top.height;
					}
					return thickness;
					
				case 2: //Left
					if (_cornerTopLeft.width >= _cornerBotLeft.width)
					{
						thickness = _cornerTopLeft.width;
					}
					else
					{
						thickness = _cornerBotLeft.width;
					}
					
					if (thickness < _left.width)
					{
						thickness = _left.width;
					}
					return thickness;
					
				case 3: //Top
					if (_cornerTopLeft.height >= _cornerTopRight.height)
					{
						thickness = _cornerTopLeft.height;
					}
					else
					{
						thickness = _cornerTopRight.height;
					}
					
					if (thickness < _top.height)
					{
						thickness = _top.height;
					}
					return thickness;
					
				default:
					return -1;
			}
		}

		public function getxSize():int
		{
			return _xSize;
		}

		public function getySize():int
		{
			return _ySize;
		}

		public function getCornerTopLeft():BitmapData
		{
			return _cornerTopLeft;
		}

		public function getCornerTopRight():BitmapData
		{
			return _cornerTopRight;
		}

		public function getCornerBotLeft():BitmapData
		{
			return _cornerBotLeft;
		}

		public function getCornerBotRight():BitmapData
		{
			return _cornerBotRight;
		}

		public function getTop():BitmapData
		{
			return _top;
		}

		public function getLeft():BitmapData
		{
			return _left;
		}

		public function getRight():BitmapData
		{
			return _right;
		}

		public function getBot():BitmapData
		{
			return _bot;
		}


	}
}